﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.LIB.YJKControls;

namespace YRevitJig
{
	public partial class PromptDlg : System.Windows.Forms.Form
	{
		public PromptDlg(IntPtr hWnd, System.Drawing.Color bkColor)
		{
			this.InitializeComponent();
			this.m_hWnd = hWnd;
			base.Location = new System.Drawing.Point(100, 100);
			this.BackColor = bkColor;
			base.Show(new JigWindowHandle(this.m_hWnd));
			base.Opacity = 0.0;
		}

		public string GetInputValue()
		{
			bool bShowInput = this.m_bShowInput;
			string result;
			if (bShowInput)
			{
				result = this.m_textInput.Text;
			}
			else
			{
				result = null;
			}
			return result;
		}

		public void SetInputValue(string strText)
		{
			bool bShowInput = this.m_bShowInput;
			if (bShowInput)
			{
				this.m_textInput.Text = strText;
			}
		}

		public void SetDlgState(string strPrompt, bool bShowPrompt, bool bShowInput)
		{
			this.m_labelPrompt.Text = strPrompt;
			System.Windows.Forms.Control labelPrompt = this.m_labelPrompt;
			this.m_bShowPrompt = bShowPrompt;
			labelPrompt.Visible = bShowPrompt;
			System.Windows.Forms.Control textInput = this.m_textInput;
			this.m_bShowInput = bShowInput;
			textInput.Visible = bShowInput;
			this.ResetDlgSize();
		}

		public void OnFocusChangeEvent(object sender, bool bHadFocus)
		{
			bool bManualFocusSignal = this.m_bManualFocusSignal;
			if (bManualFocusSignal)
			{
				this.m_bManualFocusSignal = false;
			}
			else
			{
				this.DlgVisible(bHadFocus);
			}
		}

		public void DlgVisible(bool bVisible)
		{
			base.Visible = bVisible;
			if (bVisible)
			{
				this.m_bManualFocusSignal = true;
				DllImportManeger.SetCapture(this.m_hWnd);
				DllImportManeger.SetFocus(this.m_hWnd);
			}
			else
			{
				DllImportManeger.ReleaseCapture();
			}
		}

		public void OnKeyDown(object sender, KeyEventArgs keyArg)
		{
			bool bShowInput = this.m_bShowInput;
			if (bShowInput)
			{
				bool flag = (keyArg.KeyCode >= Keys.D0 && keyArg.KeyCode <= Keys.D9) || (keyArg.KeyCode >= Keys.NumPad0 && keyArg.KeyCode <= Keys.NumPad9);
				if (flag)
				{
					string inputValue = this.GetInputValue() + this.NumberKeyString(keyArg);
					this.SetInputValue(inputValue);
				}
				else
				{
					bool flag2 = keyArg.KeyCode == Keys.Back;
					if (flag2)
					{
						string text = this.GetInputValue();
						bool flag3 = !string.IsNullOrEmpty(text);
						if (flag3)
						{
							text = text.Substring(0, text.Length - 1);
							this.SetInputValue(text);
						}
					}
				}
			}
		}

		private void m_textInput_TextChanged(object sender, EventArgs e)
		{
			bool flag = this.ResetEditSize();
			if (flag)
			{
				this.ResetDlgSize();
			}
			base.Opacity = (string.IsNullOrWhiteSpace(this.GetInputValue()) ? 0.0 : 1.0);
		}

		private string NumberKeyString(KeyEventArgs keyArg)
		{
			bool flag = keyArg.KeyCode >= Keys.D0 && keyArg.KeyCode <= Keys.D9;
			int num;
			if (flag)
			{
				num = keyArg.KeyCode - Keys.D0;
			}
			else
			{
				bool flag2 = keyArg.KeyCode >= Keys.NumPad0 && keyArg.KeyCode <= Keys.NumPad9;
				if (!flag2)
				{
					return null;
				}
				num = keyArg.KeyCode - Keys.NumPad0;
			}
			return num.ToString();
		}

		private void ResetDlgSize()
		{
			double num = 1.0;
			double num2 = 1.0;
			WinDPIManager.GetWinFormXYDpi(ref num, ref num2);
			int num3 = 1;
			bool bShowPrompt = this.m_bShowPrompt;
			if (bShowPrompt)
			{
				int num4 = (int)this.m_labelPrompt.CreateGraphics().MeasureString(this.m_labelPrompt.Text, this.Font).Width;
				this.m_labelPrompt.Location = new System.Drawing.Point(num3, 2);
				this.m_labelPrompt.Size = new Size(num4, 12);
				num3 += num4 + 2;
			}
			bool bShowInput = this.m_bShowInput;
			if (bShowInput)
			{
				int width = this.m_textInput.Width;
				this.m_textInput.Location = new System.Drawing.Point(num3, 1);
				this.m_textInput.Size = new Size(Convert.ToInt32(width), Convert.ToInt32(10.0 * num2));
				num3 += width + 1;
			}
			base.Size = new Size(Convert.ToInt32(num3), Convert.ToInt32(16.0 * num2));
		}

		private bool ResetEditSize()
		{
			int num = 30;
			int num2 = (int)this.m_textInput.CreateGraphics().MeasureString(this.m_textInput.Text, this.Font).Width;
			bool flag = num2 > this.m_textInput.Width;
			if (flag)
			{
				this.m_textInput.Width = num2;
			}
			else
			{
				bool flag2 = num2 >= this.m_textInput.Width;
				if (flag2)
				{
					return false;
				}
				bool flag3 = num2 >= num;
				if (flag3)
				{
					this.m_textInput.Width = num2;
				}
				else
				{
					this.m_textInput.Width = num;
				}
			}
			return true;
		}

		public PromptDlg.DoWhenMouseMove MouseMoveArgs;

		private bool m_bManualFocusSignal = true;

		private bool m_bShowPrompt = false;

		private bool m_bShowInput = false;

		private IntPtr m_hWnd;

		public delegate void DoWhenMouseMove();
	}
}
